﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SWSoft.Framework;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;
using MediSIIM.BizComponent.HangZhouYHQYKT;
using System.IO;

namespace AnHuiSQYXNB
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var callparam = new HangZhouYHQYKTradeParms(1401, ApplayNo(), "1001", "1587898858", "DBA", "330122D15600000500438AC76E10E1F5D12585370330122330122195209210045郑和美                        21952-09-21330122000000000000008BD03FF59");
            callparam["CARDPASS"] = "000000";

            try
            {
                YBCall(callparam);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public string ApplayNo()
        {
            //dynamic inobj = BizObjIn;
            var callparam = new HangZhouYHQYKTradeParms(1303, "", "1001", "", "DBA");
            YBCall(callparam);
            return callparam.OutValue("TRANSID");
        }

        public HangZhouYHQYKTradeParms YBCall(HangZhouYHQYKTradeParms parms)
        {
            #region 接口初始化
            string addr = "10.81.193.2";
            string servlet = "Ydtest_App/ProcessAll";
            uint point = YiBaoJKDLL.newinterface();
            YiBaoJKDLL.init(point, ref addr, 9006, ref servlet);
            YiBaoJKDLL.login(point);
            YiBaoJKDLL.enablelog(point);
            #endregion
            int retcode = YiBaoJKDLL.start(point, 290001);//
            YiBaoJKDLL.put(point, 1, "_ver", "0000");
            YiBaoJKDLL.put(point, 1, "_akc801", "330110");
            YiBaoJKDLL.put(point, 1, "_akc802", "余杭区");
            YiBaoJKDLL.put(point, 1, "_akc803", parms.AKC803);
            YiBaoJKDLL.put(point, 1, "_akb020", parms.AKB020);
            YiBaoJKDLL.put(point, 1, "_akc020", parms.AKC020);
            YiBaoJKDLL.put(point, 1, "_msgno", parms.MSGNO);
            YiBaoJKDLL.put(point, 1, "_msgid", parms.TradeNo);
            YiBaoJKDLL.put(point, 1, "_orgmsgno", parms.ORGMSGNO);
            YiBaoJKDLL.put(point, 1, "_orgmsgid", parms.ORGMSGID);
            YiBaoJKDLL.put(point, 1, "_operid", parms.OPERID);
            YiBaoJKDLL.put(point, 1, "_batno", parms.YWZQH);
            YiBaoJKDLL.put(point, 1, "_input", parms.INPUT);
            YiBaoJKDLL.put(point, 1, "_inputcount", parms.INDICATOR);
            YiBaoJKDLL.put(point, 1, "_pageno", parms.INDICATOR);//页码
            YiBaoJKDLL.put(point, 1, "_eofsign", parms.INDICATOR);//尾页标志
            YiBaoJKDLL.put(point, 1, "_workdate", parms.WORKDATE);
            YiBaoJKDLL.put(point, 1, "_refmsgid", parms.REFMSGID);
            YiBaoJKDLL.put(point, 1, "_output", parms.OUTPUT);
            YiBaoJKDLL.put(point, 1, "_outputcount", parms.OUTPUTCOUNT);
            YiBaoJKDLL.put(point, 1, "_status", parms.STATUS);
            YiBaoJKDLL.put(point, 1, "_reserve", parms.RESERVE);
            YiBaoJKDLL.setresultset(point, "input");
            foreach (var item in parms.Items.Keys)
            {
                YiBaoJKDLL.put(point, 1, item, parms[item]);
            }
            retcode = YiBaoJKDLL.run(point);
            var result = YiBaoJKDLL.getmessage(point);            
            var status = string.Empty.PadRight(100);
            var outputcount = string.Empty.PadRight(100);

            YiBaoJKDLL.getbyname(point, "_status", ref status);
            YiBaoJKDLL.getbyname(point, "_outputcount", ref outputcount);
            var list = result + "\r\n";
            label1.Text = "status:" + status;
            label2.Text = "outputcount:" + outputcount;
            if (retcode >= 0)
            {
                YiBaoJKDLL.setresultset(point, "output");
                do
                {
                    int col = 1;
                    string name = null;
                    string value = null;
                    while (true)
                    {
                        name = string.Empty.PadRight(1024);
                        value = string.Empty.PadRight(1024);
                        if (YiBaoJKDLL.getbyindex(point, col, ref name, ref value) < 0)
                        {
                            break;
                        }
                        else
                        {
                            parms.OutValue(name.ToLower().TrimEnd(' ', '\0'), value.TrimEnd(' ', '\0'));
                            list += name.TrimEnd(' ', '\0') + "=" + value.TrimEnd(' ', '\0');
                        }
                        col++;
                    }
                } while (YiBaoJKDLL.nextrow(point) > 0);
                File.WriteAllText("outdata.log", list);
            }
            else
            {
                throw new Exception("医保中心: " + result);
            }
            return parms;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Console.WriteLine("");
        }
    }
}
